/***************************************************************************
 *   Copyright (C) 2008 by Philipp Müller <philipp.mueller@gmx.de>         *
 *   Copyright (C) 2008 by Tanguy Krotoff <tkrotoff@gmail.com>             *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA          *
 ***************************************************************************/

#include <QtGui>

#include "widgetosdtv.h"
#include "channellistmodel.h"

WidgetOsdTv::WidgetOsdTv( QWidget *parent ) : QWidget( parent ),
    m_index()
{
    initUi();
}


void WidgetOsdTv::initUi()
{
    QHBoxLayout* hBox = new QHBoxLayout( this );
    QGridLayout* grid = new QGridLayout( this );

    hBox->setObjectName( QString::fromUtf8( "horizontalLayout" ) );
    grid->setObjectName( QString::fromUtf8( "gridLayout" ) );

    //Icon
    m_icon = new QLabel( this );
    m_icon->setObjectName( QString::fromUtf8( "ChannelIcon" ) );
    m_icon->setMinimumSize( QSize( 64, 48 ) );
    m_icon->setMaximumSize( QSize( 64, 48 ) );

    hBox->addWidget( m_icon );

    //Channel Name
    m_channelName = new QLabel( this );
    m_channelName->setObjectName( QString::fromUtf8( "ChannelName") );
    m_channelName->setMinimumSize( QSize( 300, 0 ) );
    m_channelName->setMaximumSize( QSize( 300, 16777215) );

    hBox->addWidget( m_channelName );

    m_epgTime = new QLabel( this );
    m_epgTime->setObjectName( QString::fromUtf8( "epgTime") );
    m_epgTime->setMinimumSize( QSize( 45, 0 ) );
    m_epgTime->setMaximumSize( QSize( 45, 16777215 ) );

    grid->addWidget( m_epgTime, 0, 0, 1, 1 );

    m_epgTitle = new QLabel( this );
    m_epgTitle->setObjectName( QString::fromUtf8( "epgTitle" ) );

    grid->addWidget( m_epgTitle, 0, 1, 1, 1 );

    m_epgNextTime = new QLabel( this );
    m_epgNextTime->setObjectName( QString::fromUtf8( "epgNextTime") );
    m_epgNextTime->setMinimumSize( QSize( 45, 0 ) );
    m_epgNextTime->setMaximumSize( QSize( 45, 16777215 ) );

    grid->addWidget( m_epgNextTime, 1, 0, 1, 1 );

    m_epgNextTitle = new QLabel( this );
    m_epgNextTitle->setObjectName( QString::fromUtf8( "epgNextTitle" ) );

    grid->addWidget( m_epgTitle, 0, 1, 1, 1 );

    hBox->addStretch();
    hBox->addLayout( grid );

    setLayout( hBox );
}


void WidgetOsdTv::setIndex( const QModelIndex &index )
{
    m_index = index;
}


void WidgetOsdTv::updateOsdTv( const QModelIndex & topLeft )
{
    if ( !m_index.isValid() )
    {
        return;
    }
    if ( topLeft.row() != m_index.row() )
    {
        return;
    }
    repaint();
}


void WidgetOsdTv::paintEvent( QPaintEvent* )
{
    if( !m_index.isValid() )
    {
        qWarning() << "WidgetOsdTv::paintEvent but index not set";
        return;
    }
    QString channelName    = m_index.model()->data( m_index, ChannelListModel::ChannelNameRole ).toString();
    QString iconPath       = m_index.model()->data( m_index, ChannelListModel::ChannelIconRole ).toString();

    QString epgTitle       = m_index.model()->data( m_index, ChannelListModel::ChannelEpgTitleRole ).toString();
    QDateTime epgTime      = m_index.model()->data( m_index, ChannelListModel::ChannelEpgTimeRole ).toDateTime();
    quint32 epgDuration    = m_index.model()->data( m_index, ChannelListModel::ChannelEpgDurationRole ).toUInt();

    QString epgTimeString;
    if ( epgDuration > 0 )
    {
        epgTimeString = epgTime.toString( "hh:mm" );
    }

    QString epgNextTitle       = m_index.model()->data( m_index, ChannelListModel::ChannelEpgNextTitleRole ).toString();
    QDateTime epgNextTime      = m_index.model()->data( m_index, ChannelListModel::ChannelEpgNextTimeRole ).toDateTime();
    quint32 epgNextDuration    = m_index.model()->data( m_index, ChannelListModel::ChannelEpgNextDurationRole ).toUInt();

    QString epgNextTimeString;
    if ( epgNextDuration > 0 )
    {
        epgNextTimeString = epgNextTime.toString( "hh:mm" );
    }

    QPainter painter( this );
    painter.save();

    int x = 0/*this->x()*/;
    int y = 0/*this->y()*/;
    int width = this->width();
    int height = this->height();

    QRect rect;

//     //Background
//     rect = QRect( 0, 0, width, height );
//
// //     painter.setOpacity( 0.5 ); //Don't do this for performance reasons
//
//     QColor c = Qt::cyan;
//     c.setAlphaF( c.alphaF() * 0.5 );
//     painter.fillRect( rect, QBrush( c ) );

    //Icon 64x48
    painter.drawImage( x, y , QImage( iconPath ) );

    //Channel
    painter.setFont( QFont( QString(), 15 ) );

    rect = QRect( x + 70, y + 6, 200, 22 );
    painter.drawText( rect, Qt::AlignLeft | Qt::AlignTop, channelName );

    painter.setFont( QFont( QString(), 12 ) );

    // Time
    rect = QRect( x + 280, y + 3, 50, 22 );
    painter.drawText( rect, Qt::AlignRight | Qt::AlignTop, epgTimeString );

    // Title
    rect = QRect( x + 280 + 55, y + 3, width - 280 - 55, 22 );
    painter.drawText( rect, Qt::AlignLeft | Qt::AlignTop, epgTitle );

    // Next Time
    rect = QRect( x + 280, y + 3 + 22, 50, 22 );
    painter.drawText( rect, Qt::AlignRight | Qt::AlignTop, epgNextTimeString );

    // Next Title
    rect = QRect( x + 280 + 55, y + 3 + 22, width - 280 - 55, 22 );
    painter.drawText( rect, Qt::AlignLeft | Qt::AlignTop, epgNextTitle );

    painter.restore();
}


